apiready = function() {
seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper"], function(V, $api, $, app, service, Swiper) {
    var el = document.querySelector('.container');
    var vm = {
        el: el,
        data: {
            showDio: false, // 显示综合搜索
            addActive: false, // 添加活动class
            isChoicCount: true,
            myCity: '广州', // 所在城市
            is_yzc: false, // 是否为yzc商品
            isChoicPrice: false, // 是否选中了价格
            priceChoic: false, // 价格升、降
            showScreen: false, // 价格、城市筛选
            showLocation: false, // 城市、地区选择
            selectYZC: false, // 是否选中yzc
            searchStr: null,
            page: 1,
            searchVal: '', // 搜索关键字
            hotList: [], // 搜索出的商品列表
            locationList: {}, // 城市、省份列表
            searchMsg: {}, // 搜索条件
            userInfo: '', // 获取用户身份
            hasMoreData: true,
            map: '',
            bs: ""  //ajax弹出服务器提示控制变量
        },
        created: function() {
            var that = this;
            if ($api.getStorage("user")) {
                this.userInfo = $api.getStorage("user").data;
                this.bs = 1;
            }

            this.searchVal = api.pageParam.searchVal;

            // var map = api.require('bMap');
            // map.initMapSDK(function(ret) {
            //     if (ret.status) {
            //         console.log('初始化成功');
            //
            //     }
            // });
            // that.getLocation();
        },
        mounted: function() {
            var that = this;
            var map = api.require('bMap');
            if (api.systemType == "ios") {
                map.initMapSDK(function(ret) {
                    if (ret.status) {
                        console.log('百度地图成功初始化');
                    } else {
                        console.log('百度地图初始化不成功');
                    }
                });
            }


            $('#search-val').val(that.searchVal);
            var winHeight = api.winHeight - 115;
            $('.search-screen').css('max-height', winHeight);
            $('#search-val').on('input', function() {
                that.searchVal = $(this).val();
            });
            this.search(1, 0, 0, null);

            //获取省份和城市
            this.getSearchLocation();

            // 加载更多
            api.addEventListener({
                name: 'scrolltobottom',
                extra: {
                    threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                }
            }, function(ret, err) {
                that.scrolltobottom()
            });
            this.$nextTick(function(){
              $('.container').slideDown(200)
            })
        },
        methods: {
            // 筛选
            changeNav: function(type) {
                $('.nav-item').removeClass('active');
                $('.nav-item').eq(type).addClass('active');
                this.isChoicPrice = false;
                this.isChoicCount = false;
                if (type == 0) {
                    // 综合
                    this.showDio = !this.showDio;
                    this.showScreen = false;
                } else if (type == 1) {
                    // 销量
                    this.hotList = [];
                    this.page = 1;
                    this.priceChoic = false;
                    this.showDio = false;
                    this.showScreen = false;
                    this.addActive = !this.addActive;
                    this.search(0, 2, 0);
                } else if (type == 2) {
                    // 价格
                    this.hotList = [];
                    this.page = 1;
                    this.isChoicPrice = true;
                    this.showDio = false;
                    this.showScreen = false;
                    this.priceChoic = !this.priceChoic;
                    if (this.priceChoic) {
                        // 升序排序
                        this.search(0, 0, 1);
                    } else {
                        // 降序排序
                        this.search(0, 0, 2);
                    }
                } else if (type == 3) {
                    // 筛选
                    this.priceChoic = false;
                    this.showScreen = !this.showScreen;
                    this.showDio = false;
                    $('.nav-item').eq(type).addClass('sw-active');
                }
            },

            // 综合
            choicFirst: function(type) {
                this.hotList = [];
                this.page = 1;
                var elObj = document.querySelectorAll('.flow-dio div');
                elObj.forEach(function(item, index) {
                    item.classList.remove('active');
                });
                elObj[type].classList.add('active');
                if (type == 0) {
                    // 综合排序
                    this.search(1, 0, 0);
                } else if (type == 1) {
                    // 评分排序
                    this.search(2, 0, 0);
                }
            },

            // 选择发货地
            selectLocation: function(e) {
                var el = document.querySelectorAll('.city-list ul li');
                if (e.target.nodeName == 'LI') {
                    el.forEach(function(item, index) {
                        item.classList.remove('active-li');
                    });
                    e.target.classList.add('active-li');
                    var locationName = $('.city-list ul li.active-li').text();
                    this.myCity = locationName;
                }
            },

            // 搜索
            search: function(main, sale, price) {
                var that = this;
                var val = $('#search-val').val();
                if (val == '' || val == null) {
                  if(!api.pageParam.shopData){
                    api.toast({
                        msg: '请输入搜索内容',
                        duration: 2000,
                        location: 'middle'
                    });
                    return;
                  }

                }

                var def = {}
                    // 判断是否搜索yzc商品
                if (this.selectYZC) {
                    that.is_yzc = true;
                    def = {
                        search: val,
                        page: that.page,
                        is_yzc: 1
                    }
                } else {
                    that.is_yzc = false;
                    def = {
                        search: val,
                        keyword:val,
                        page: that.page,
                        is_yzc: 0
                    }
                }

                var choic_obj = {
                    main_sort: main,
                    sale_sort: sale,
                    price_sort: price,
                    unionid:api.pageParam.shopData?api.pageParam.shopData.unionid:'',
                    shop_code:api.pageParam.shopData?api.pageParam.shopData.shop_code:''
                }
                var obj = $.extend(def, choic_obj),url=service['searchMain'];

                that.searchMsg = obj;
                if (that.searchStr) {
                    that.searchMsg['search_area'] = that.searchStr;
                }

                if(api.pageParam.shopData){
                  // url=service['searchGoods']
                  var shop_data = api.pageParam.shopData;

                  // alert(JSON.stringify()$api.getStorage('shop_data')))
                  // return;
                  var dataObj={
                    values: that.searchMsg,
                    shop_code: shop_data.shop_code,
                    unionid: shop_data.unionid
                  }
                }else{
                  var dataObj={
                    values: that.searchMsg
                  }
                }
                app.ajax(
                    url, {
                        data: dataObj
                    },
                    function(res) {
                        if (res.code == 0) {
                            if (res.data.length > 0) {
                                res.data.forEach(function(item, index) {
                                    that.hotList.push(item);
                                });
                                that.hasMoreData = true;
                            } else {
                                that.hasMoreData = false;
                                api.toast({
                                    msg: '没有更多数据了',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'middle'
                            });
                        }
                    }
                );
            },

            // 加载跟多
            scrolltobottom: function() {
                var lastSearch = this.searchMsg;
                if (this.hasMoreData) {
                    this.page += 1;
                    this.search(lastSearch.main_sort, lastSearch.sale_sort, lastSearch.price_sort);
                }
            },

            // 定位
            getLocation: function() {
                var that = this;
                var map = api.require('bMap');
                map.getLocation({
                    accuracy: '100m',
                    autoStop: true,
                    filter: 1
                }, function(ret, err) {
                    if (ret.status) {
                        //获取位置信息成功
                        var jd = ret.lon;
                        var wd = ret.lat;

                        app.mask();

                        map.getNameFromCoords({
                            lon: jd,
                            lat: wd
                        }, function(res, err) {
                            app.unmask();
                            if (res.status) {
                                var city = res.city;
                                that.myCity = city;
                            } else {
                                api.toast({
                                    msg: '定位失败',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });

                    } else {
                        api.toast({
                            msg: '定位失败',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            },

            // 是否选中yzc
            choicYzc: function() {
                this.selectYZC = !this.selectYZC;
            },

            // 获取省份和城市
            getSearchLocation: function() {
                var that = this;
                app.ajax(
                    service['searchGetSearchLocation'], {
                        data: {
                            values: {}
                        }
                    },
                    function(res) {
                        if (res.code == 0) {
                            that.locationList = res.data;
                        } else {
                            api.toast({
                                msg: res.message,
                                duration: 2000,
                                location: 'middle'
                            });
                        }
                    }
                );
            },

            // 选择地区
            selectCity: function() {
                var locationName = $('.city-list ul li.active-li').text();
                var startPrice = $('.start-price').val();
                var endPrice = $('.end-price').val();
                startPrice ? startPrice : startPrice = 0;
                endPrice ? endPrice : endPrice = 0;
                var str = startPrice + ',' + endPrice + '|' + locationName;
                var obj = this.searchMsg
                this.hotList = [];
                this.page = 1;
                this.searchStr = str;
                this.search(obj.main_sort, obj.sale_sort);
                this.showScreen = false;
            },

            // 重置
            reset: function() {
                $('.nav-item').eq(3).removeClass('sw-active');
                $('.city-list ul li').removeClass('active-li');
                this.selectYZC = false;
                this.searchStr = false;
                this.getLocation();
            },

            // 清空搜索框
            clearInput: function() {
                $('#search-val').val('');
            },

            // 监听手机上的搜索按钮
            doSearch: function() {
                this.hotList = [];
                this.page = 1;
                this.search();
            },

            closeMask: function() {
                this.showScreen = false;
            },
            locationTogg: function() {
                this.showLocation = !this.showLocation;
            },
            // 前往商品详情
            toGoodsDetail: function(goodsId, isGroup, isYzc) {
                if (isYzc) {
                    app.openWin('buyGoodsYzc', {
                        goodsId: goodsId
                    });
                } else {
                    if (isGroup) {
                        // 拼团商品
                        app.openWin('groupBuyDetail', {
                            goodsId: goodsId,
                            reload:true
                        });
                    } else {
                        // 普通商品
                        app.openWin('buyGoods', {
                            goodsId: goodsId},{reload:true});
                    }
                }
            },

            // 关闭当前页面
            back: function() {
                api.closeWin({
                    name: 'buyGoods'
                });
                api.closeWin({
                    name: 'buyGoodsYzc'
                });
                api.closeWin();
            }
        }
    }
    var even = function() {}
    var newVue = new Vue(vm);
    even();
});
}
